package zemberek3.lexicon.lexgraph;

import com.google.common.collect.Sets;
import zemberek3.lexicon.LexicalForm;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * Represents a lexical node in lex-graph.
 */
public class LexNode {
    String id;
    LexicalForm form;
    NodeType nodeType;

    Set<LexNode> incoming = new HashSet<LexNode>();
    Set<LexNode> outgoing = new HashSet<LexNode>();

    public LexNode(String id, LexicalForm form) {
        this.id = id;
        this.form = form;
        this.nodeType = NodeType.TRANSFER;
    }

    public LexNode(String id, LexicalForm form, NodeType nodeType) {
        this.id = id;
        this.form = form;
        this.nodeType = nodeType;
    }

    public LexNode addIn(LexNode... nodes) {
        Collections.addAll(incoming, nodes);
        return this;
    }

    public LexNode addIn(Iterable<LexNode>... iterables) {
        for (Iterable<LexNode> iterable : iterables) {
            for (LexNode lexNode : iterable) {
                incoming.add(lexNode);
            }
        }
        return this;
    }

    public LexNode addOut(LexNode... nodes) {
        Collections.addAll(outgoing, nodes);
        return this;
    }

    public LexNode removeOut(LexNode... nodes) {
        outgoing.removeAll(Arrays.asList(nodes));
        return this;
    }

    public LexNode addOut(Iterable<LexNode>... iterables) {
        for (Iterable<LexNode> iterable : iterables) {
            for (LexNode lexNode : iterable) {
                outgoing.add(lexNode);
            }
        }
        return this;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        LexNode lexNode = (LexNode) o;

        return id.equals(lexNode.id);
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }
}
